package cn.com.youyuan.common;
//#ifdef HasLog
import cn.com.youyuan.SDK.common.Logger;
//#endif

import cn.com.youyuan.SDK.interfaces.DataStorageAgent;

public class RmsManager implements DataStorageAgent
{
	private static final int STORAGE_SIZE = 13;

	private RmsObjectStorage[] m_storages = new RmsObjectStorage[STORAGE_SIZE];

	public byte[][] get(int dataType)
	{
		if (m_storages[dataType] == null)
		{
			m_storages[dataType] = new RmsObjectStorage(String.valueOf(dataType), J2meStrRes.ZERO);
		}

		return m_storages[dataType].readAll();
	}

	public String getVersion(int dataType)
	{
		if (m_storages[dataType] == null)
		{
			m_storages[dataType] = new RmsObjectStorage(String.valueOf(dataType), J2meStrRes.ZERO);
		}
		
		return m_storages[dataType].getVersion();
	}

	public void put(int dataType, String key, byte[] data)
	{
		if (m_storages[dataType] == null)
		{
			m_storages[dataType] = new RmsObjectStorage(String.valueOf(dataType), J2meStrRes.ZERO);
		}
		
		m_storages[dataType].save(data, key);
	}

	public void put(int dataType, String[] keys, byte[][] datas)
	{
		if (m_storages[dataType] == null)
		{
			m_storages[dataType] = new RmsObjectStorage(String.valueOf(dataType), J2meStrRes.ZERO);
		}
		
		m_storages[dataType].save(datas, keys);
	}

	public void close()
	{
		for (int i = 0; i < m_storages.length; i++)
		{
			if (m_storages[i] != null)
			{
				m_storages[i].close();
			}
		}
	}

	public void remove(int dataType, String key)
	{
		if (m_storages[dataType] != null)
		{
			m_storages[dataType].delete(key);
		}
	}

	public void remove(int dataType)
	{
		try
		{
			if (m_storages[dataType] != null)
			{
				m_storages[dataType].clear();
			}
		}
		catch (Exception e)
		{
			//#ifdef HasLog
			log(getClass(), e);
			//#endif
		}
	}

	public void removeAll()
	{
		try
		{
			for (int i = 0; i < m_storages.length; i++)
			{
				if (m_storages[i] == null)
				{
					m_storages[i] = new RmsObjectStorage(String.valueOf(i), J2meStrRes.ZERO);
				}
				
				m_storages[i].clear();
				//#ifdef HasLog
				log(getClass(), "clear rms " + i);
				//#endif
			}
		}
		catch (Exception e)
		{
			//#ifdef HasLog
			log(getClass(), e);
			//#endif
		}
	}

	public void setVersion(int dataType, String version)
	{
		if (m_storages[dataType] != null)
		{
			m_storages[dataType].setVersion(version);
			//#ifdef HasLog
			log(getClass(), "set rms" + dataType + "'s version to " + version);
			//#endif
		}
		
		
		
	}
	
	//#ifdef HasLog
	private void log(Class c, String s)
	{
		Logger.addLog(c, s);
	}

	private void log(Class c, Exception e)
	{
		Logger.addLog(c, e);
	}

	//#endif
}